/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlets;

import BancoDados.Profissional;
import BancoDados.Usuario;
import Sistema.GerenciadorServlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created on : May 21, 2011, 2:23:59 PM
 * @author diogo
 */
@WebServlet(name="ServletProfissional", urlPatterns={"/ServletProfissional"})
public class ServletProfissional extends HttpServlet {
   // <editor-fold defaultstate="collapsed" desc="netbeans default template">
////    /**
////     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
////     * @param request servlet request
////     * @param response servlet response
////     * @throws ServletException if a servlet-specific error occurs
////     * @throws IOException if an I/O error occurs
////     */
////    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
////    throws ServletException, IOException {
////        response.setContentType("text/html;charset=UTF-8");
////        PrintWriter out = response.getWriter();
////        try {
////            /* TODO output your page here
////            out.println("<html>");
////            out.println("<head>");
////            out.println("<title>Servlet ServletProfissional</title>");
////            out.println("</head>");
////            out.println("<body>");
////            out.println("<h1>Servlet ServletProfissional at " + request.getContextPath () + "</h1>");
////            out.println("</body>");
////            out.println("</html>");
////            */
////        } finally {
////            out.close();
////        }
////    }
	// </editor-fold>

    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        StringBuffer output = new StringBuffer("");


        response.getWriter().print(output);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        GerenciadorServlet.Codificar(request, response);
		StringBuilder output = new StringBuilder("");
		System.out.println("post....");
		String tipo = request.getParameter("tipo");
		if (tipo.equals("inserir")) {
			boolean resultado;
			resultado = Profissional.Inserir(request);
			output.append(resultado);

		} else if (tipo.startsWith("check")){
			String toCheckValue = request.getParameter("valor");
			if (tipo.endsWith("CPF")){
				// CPF no tamanho certo?
				int length = toCheckValue.length();
				if (length == 14){
					// sim, entao conecta no banco e verifica se tem cpf...
					boolean  existe = false;
					existe = Profissional.ExisteCpf(toCheckValue);

					// CPF ja existe?
					if (existe) {
						// sim, entao diz q n pode usar
						output.append(Sistema.GeradorPaginas.MessageError(String.format("O CPF \"%s\" ja esta sendo usado.", toCheckValue)));

					} else {
						output.append(Sistema.GeradorPaginas.MessageSuccess(String.format("O CPF \"%s\" pode ser usado!", toCheckValue)));
						
					}
				} else {
					// nao, entao fala q n tem tamanho certo.
					output.append(Sistema.GeradorPaginas.MessageError(String.format("o CPF deve conter 14 letras! (faltam %d)", 14-length )));
				}

			} 
			else if(tipo.endsWith("Login")){
				boolean resultado = Usuario.ExisteLogin(toCheckValue);
				
				// Login ja existe?
				if (resultado == true){
					// sim, entao fala q n pode usar
					output.append(Sistema.GeradorPaginas.MessageError(String.format("O login \"%s\" nao pode ser usado.", toCheckValue)));
				} else {
					output.append(Sistema.GeradorPaginas.MessageSuccess(String.format("O login \"%s\" pode ser usado!.", toCheckValue)));
				}
			}
			
        response.getWriter().print(output);
		}
    }

    // <editor-fold defaultstate="collapsed" desc="netbeans default template">
    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
////    @Override
////    public String getServletInfo() {
////        return "Short description";
////    }
    // </editor-fold>
}
